package workspace;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt();//zhengshu
            int k = scanner.nextInt();//ronglaing
            Set<Integer> set = new HashSet<>();
            Boolean [] used = new Boolean[n+1];

            int [] in = new int[n];
            int [] out = new int[n];
            for (int j = 0; j < n; j++) {
                in[j] = scanner.nextInt();
                used[j] = false;
            }
            used[n] = false;
            for (int j = 0; j < n; j++) {
                out[j] = scanner.nextInt();
            }
            for (int j = 0; j < k; j++) {
                set.add(in[j]);
                used[in[j]] = true;
            }
            int res = 0;
            for (int j = 0; j < n; j++) {
                if(set.contains(out[j])){
                    set.remove(out[j]);
                    int num = k;

                    while(num<n&&used[in[num]]){
                        num++;
                    }
                    if(num==n){
                        break;
                    }
                    used[in[num]]=true;
                    set.add(in[num]);
                }else{
                    res++;
                    used[out[j]] = true;
                }
            }
            System.out.println(res);
        }
    }
}
